package com.brsya.movie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.brsya.base.base.BaseMvpFragment;
import com.brsya.base.base.Contents;
import com.brsya.base.bean.InventoryDetailBean;
import com.brsya.base.bean.MovieBean;
import com.brsya.base.recycler.BaseAdapter;
import com.brsya.base.util.LoggerUtil;
import com.brsya.base.util.UiUtil;
import com.brsya.movie.R;
import com.brsya.movie.activity.PlayActivity;
import com.brsya.movie.adapter.SearchListAdapter;
import com.brsya.movie.contract.SearchContract;
import com.brsya.movie.presenter.SearchPresenter;

import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/7/7  15:10
 * Description:
 * 搜索列表
 */
public class SearchListFragment extends BaseMvpFragment<SearchPresenter> implements SearchContract.View {
    private SwipeRefreshLayout srlRefresh;
    private RecyclerView rlList;
    private TextView bottomTextView;
    private LinearLayout llToTop;


    private SearchListAdapter adapter;

    /**
     * 搜索内容
     */
    private String searchText;

    /**
     * 页码
     */
    private int pageNumber;
    /**
     * 是否初次进入
     */
    private boolean isFirst = true;

    private OnSearchResultListener onSearchResultListener;

    public static SearchListFragment newInstance(String searchText) {

        Bundle args = new Bundle();
        args.putString("searchText", searchText);
        SearchListFragment fragment = new SearchListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setArguments(@Nullable Bundle args) {
        super.setArguments(args);
        if (args != null) {
            searchText = args.getString("searchText");
            if (!isFirst) {
                loadData(true);
            }
        }
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_search);

        srlRefresh = findViewById(R.id.srl_refresh);
        rlList = findViewById(R.id.rl_list);
        llToTop = findViewById(R.id.ll_to_top);
    }

    @Override
    protected void initData() {
        isFirst = false;


        llToTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rlList.smoothScrollToPosition(0);
            }
        });


        srlRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData(true);
            }
        });

        rlList.setLayoutManager(new LinearLayoutManager(getContext()));

        rlList.setAdapter(adapter = new SearchListAdapter(getContext()));

        View bottomView = LayoutInflater.from(getActivity()).inflate(R.layout.item_no_more_data, null, false);
        bottomTextView = bottomView.findViewById(R.id.tv_no_more_data_text);
        adapter.setBottomView(bottomView);


        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<MovieBean>() {
            @Override
            public void onItemClick(int position, MovieBean data) {
                Intent intent = new Intent(getActivity(), PlayActivity.class);
                intent.putExtra(Contents.INTENT_MOVIE_DETAIL, data);
                startActivity(intent);
            }
        });

        rlList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (adapter.getDataList().size() == 0) {
                    return;
                }
                if (UiUtil.isSlideToBottom(recyclerView)) {
                    loadData(false);
                }
            }
        });

    }

    @Override
    protected void initPresenter() {
        presenter = new SearchPresenter();
        presenter.attachView(this);
    }

    @Override
    public void showSearchResult(List<MovieBean> movieList) {
        if (movieList != null && movieList.size() > 0) {
            if (pageNumber == 1) {
                if (onSearchResultListener != null) {
                    onSearchResultListener.onSearchResult(movieList.get(0));
                }
                adapter.setDataList(movieList);
            } else {
                adapter.addData(movieList);
            }
            adapter.notifyDataSetChanged();
        } else {
            showNoMore();
        }

    }

    @Override
    public void showNoMore() {
        if (bottomTextView != null) {
            bottomTextView.setText(getString(R.string.list_no_more_data));
        }
    }

    @Override
    public void showLoading() {
        srlRefresh.setRefreshing(true);
        if (bottomTextView != null) {
            bottomTextView.setText(getString(R.string.list_load_more_data));
        }
    }

    @Override
    public void hideLoading() {
        srlRefresh.setRefreshing(false);
    }

    /**
     * 加载数据
     *
     * @param isFirst 是否加载第一页
     */
    private void loadData(boolean isFirst) {
        if (searchText == null) {
            return;
        }
        if (isFirst) {
            pageNumber = 1;
        } else {
            pageNumber++;
        }
        if (presenter != null) {
            LoggerUtil.e("加载数据:" + pageNumber);
            presenter.getMovieList(pageNumber, searchText);
        }
    }

    /**
     * 清除数据
     */
    public void clearData() {
        adapter.getDataList().clear();
        adapter.notifyDataSetChanged();
    }

    public void setOnSearchResultListener(OnSearchResultListener onSearchResultListener) {
        this.onSearchResultListener = onSearchResultListener;
    }

    /**
     * 获取到搜索结果监听
     */
    public interface OnSearchResultListener {
        /**
         * 获取到搜索结果
         *
         * @param bean 第一条数据
         */
        void onSearchResult(MovieBean bean);
    }
}
